# -*- coding: utf-8 -*-
# Description: 应用配置文件
# Created: dingwenjing 2022/11/29

import os

basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
    """默认配置
    """
    # 应用参数
    APP_NAME = 'performance_prediction'
    SERVER_PORT = 8081
    #
    FLATPAGES_AUTO_RELOAD = True
    FLATPAGES_EXTENSION = '.md'
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'can you guess it'
    DEBUG = True

    # SQLALCHEMY配置
    SQLALCHEMY_TRACK_MODIFICATIONS = False  # 屏蔽 sql alchemy 的 FSADeprecationWarning
    SQLALCHEMY_ENGINE_OPTIONS = {
        'pool_recycle': 3600,
        'pool_pre_ping': True,
        'max_overflow': 36
    }

    # 是否开启任务调度器,默认不开启
    SCHEDULER_OPEN = False
    SCHEDULER_API_ENABLED = False
    # 任务调度器lock文件名称
    SCHEDULER_LOCK_FILE_NAME = 'scheduler-{}.lock'.format(APP_NAME)

    TRIAN_LENGTH = 5

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    """开发环境配置
    """
    # # 数仓连接
    ODS_DB2_HOST = '10.76.2.91'
    ODS_DB2_PORT = 50091
    ODS_DB2_USER = 'iplat4j'
    ODS_DB2_PASSWD = 'iplat4j'
    ODS_DB2_DB = 'dbprod20'
    ODS_DB2_CHARSET = 'utf8'
    # # 数仓连接
    ODS_DB2_HOST_2 = '10.76.4.57'
    ODS_DB2_PORT_2 = 50000
    ODS_DB2_USER_2 = 'bwbaraqa01'
    ODS_DB2_PASSWD_2 = 'scefke9961'
    ODS_DB2_DB_2 = 'TG0PROD'
    ODS_DB2_CHARSET_2 = 'utf8'
    # # 数仓连接
    # ODS_DB2_HOST = '10.7  0.83.76'
    # ODS_DB2_PORT = 50000
    # ODS_DB2_USER = 'dbtest03'
    # ODS_DB2_PASSWD = 'mHA4j7pFRq'
    # ODS_DB2_DB = 'dbtest03'
    # ODS_DB2_CHARSET = 'utf8'
    # sqlalchemy ORM底层所访问数据库URI，可选（不使用时可删除）
    SQLALCHEMY_DATABASE_URI = 'db2+ibm_db://{}:{}@{}:{}/{}?charset={}'.format(
        ODS_DB2_USER, ODS_DB2_PASSWD, ODS_DB2_HOST, ODS_DB2_PORT, ODS_DB2_DB, ODS_DB2_CHARSET)
    SQLALCHEMY_BINDS = {'db_tg_RDS':'jdbc:db2://bwbaraqa01:scefke9961@10.76.4.57:50000/TG0PROD?charset=utf8'}
    # Redis配置，可选（不使用时可删除）
    REDIS_HOST = ''
    REDIS_PORT = '6379'
    REDIS_PASSWD = ''
    REDIS_MAX_CONNECTIONS = 5
    # Celery配置，可选（不使用时可删除）
    BROKER_URL = 'amqp://guest:guest@127.0.0.1:5672/celery_demo_host'  # 使用 RabbitMQ中间件，也可以用redis作为中间件
    default_timeout = 60 * 60
    CELERY_BROKER_URL = BROKER_URL
    # CELERY_RESULT_BACKEND = BROKER_URL
    # 导入任务所在的模块
    CELERY_IMPORTS = 'app.task.celery_task'
    CELERY_DEFAULT_QUEUE = "flask_demo"
    # 设置定时任务
    from datetime import timedelta
    from celery.schedules import crontab
    CELERY_TIMEZONE = 'Asia/Shanghai'  # 指定时区，不指定默认为 'UTC'
    # crontab()配置定时规则,参考https://docs.celeryq.dev/en/stable/userguide/periodic-tasks.html
    CELERYBEAT_SCHEDULE = {
        'cron_task_demo': {
            'task': 'app.task.celery_task.cron_task_demo',
            'schedule': crontab(minute='*/1'),
            'args': None
        }

    }




class TestConfig(Config):
    """测试环境配置
    """
    pass


class UatConfig(Config):
    pass


class ProductionConfig(Config):
    """生产环境配置
    """
    pass


# 设置环境配置映射
config = {
    'development': DevelopmentConfig,
    'test': TestConfig,
    'uat': UatConfig,
    'production': ProductionConfig,
    'default': DevelopmentConfig
}


def get_active_config():
    """获取当前生效的环境配置类

    :return: 当前生效的环境配置类
    """
    config_name = os.getenv('FLASK_CONFIG') or 'default'
    return config[config_name]


def get_active_config_name():
    """获取当前生效的环境配置名称

    :return: 当前生效的环境配置名称
    """
    config_name = os.getenv('FLASK_CONFIG') or 'default'
    return config_name
